package com.gavindon.android.base

import com.gavindon.android.entity.BaseResp
import com.gavindon.android.network.*
import java.net.ConnectException
import java.net.SocketTimeoutException
import java.net.UnknownHostException

/**
 * description:
 * Created by liNan on  2021/5/6 16:15
 */
abstract class BaseRepository {

    val service = RetrofitHelp.create()

    //泛型具体化
    suspend inline fun <reified T : Any> launchRequest(
        isShowLoading: Boolean = false,
        crossinline block: suspend () -> BaseResp<T>
    ): DataResult<T> {
        return try {
            if (isShowLoading) LoadingHelper.createLoading()
            val resp = block()
            if (isShowLoading) LoadingHelper.cancelLoading()
            if (resp.success) {
                if (resp.data is List<*>) {
                    if ((resp.data as List<*>).isNullOrEmpty()) {
                        DataResult.Empty<T>(resp.data)
                    } else {
                        DataResult.Success<T>(resp.data)
                    }
                } else {
                    DataResult.Success(resp.data)
                }
            } else {
                throw (ServerMsgError(resp.msg))
//                DataResult.Failed(resp.data)
            }
        } catch (e: Exception) {
            ErrorManager.handleException(e)
            LoadingHelper.cancelLoading()
            when (e) {
                is SocketTimeoutException -> {
                    //服务器响应的超时
                    DataResult.NetTimeOut(e)
                }
                is ConnectException -> {
                    ////连接服务器超时
                    DataResult.NetTimeOut(e)
                }
                is UnknownHostException -> {
                    ////连接服务器超时
                    DataResult.NetTimeOut(e)
                }
                else -> {
                    DataResult.Error(e)
                }
            }
        }
    }


    //泛型具体化
    suspend inline fun <reified T : Any> launchAutoRefreshRequest(
        isShowLoading: Boolean = true,
        crossinline block: suspend () -> BaseResp<T>
    ): DataResult<T> {
        return try {
//            if (isShowLoading) LoadingHelper.createLoading()
            val resp = block()
//            if (isShowLoading) LoadingHelper.cancelLoading()
            if (resp.success) {
                if (resp.data is List<*>) {
                    if ((resp.data as List<*>).isNullOrEmpty()) {
                        DataResult.Empty<T>(resp.data)
                    } else {
                        DataResult.Success<T>(resp.data)
                    }
                } else {
                    DataResult.Success(resp.data)
                }
            } else {
//                DataResult.Success(resp.data)
                DataResult.Error(ServerMsgError(resp.msg))
            }
        } catch (e: Exception) {
            //不进行异常提示
//            ErrorManager.handleException(e)
//            LoadingHelper.cancelLoading()
            when (e) {
                is SocketTimeoutException -> {
                    //服务器响应的超时
                    DataResult.NetTimeOut(e)
                }
                is ConnectException -> {
                    ////连接服务器超时
                    DataResult.NetTimeOut(e)
                }
                is UnknownHostException -> {
                    ////连接服务器超时
                    DataResult.NetTimeOut(e)
                }
                else -> {
                    DataResult.Error(e)
                }
            }
        }
    }

    suspend inline fun <reified T : Any> launchRequest2(
        isShowLoading: Boolean = true,
        crossinline block: suspend () -> BaseResp<T>
    ): DataResult<BaseResp<T>> {
        return try {
//            if (isShowLoading) LoadingHelper.createLoading()
            val resp = block()
//            if (isShowLoading) LoadingHelper.cancelLoading()
            if (resp.success) {
                if (resp.data is List<*>) {
                    if ((resp.data as List<*>).isNullOrEmpty()) {
                        DataResult.Empty(resp)
                    } else {
                        DataResult.Success(resp)
                    }
                } else {
                    DataResult.Success(resp)
                }
            } else {
                throw (ServerMsgError(resp.msg))
//                DataResult.Failed(resp.data)
            }
        } catch (e: Exception) {
            ErrorManager.handleException(e)
//            LoadingHelper.cancelLoading()
            when (e) {
                is SocketTimeoutException -> {
                    //服务器响应的超时
                    DataResult.NetTimeOut(e)
                }
                is ConnectException -> {
                    ////连接服务器超时
                    DataResult.NetTimeOut(e)
                }
                is UnknownHostException -> {
                    ////连接服务器超时
                    DataResult.NetTimeOut(e)
                }
                else -> {
                    DataResult.Error(e)
                }
            }
        }
    }
}
